#!/bin/sh
#name:Show agent list
#desc:Show Agent List
#package:oddiscovery
#type:local

# Copyright(c) 2011 OpenDomo Services SL. Licensed under GPL v3 or later

ALIST=/var/opendomo/oddiscovery-agentlist
REXEC=/usr/local/opendomo/discovery/execRemoteScript.sh
HOME=/home/$USER
mkdir -p $HOME/.ssh
touch $HOME/.ssh/known_hosts

if test -z "$1"; then
	# -------------------------------------------------------------------------
	# No params - Input Form
	# -------------------------------------------------------------------------
	echo "#> Active agents:"
	echo "list:`basename $0`"
	for agent in `cat $ALIST | grep -v "AGENT-DOWN" | cut -f1 -s`; do
		if grep -q $agent $HOME/.ssh/known_hosts; then
			echo "	-$agent	$agent	odagent"
		else
			echo "	-$agent	$agent	foreigner"		
		fi
	done
	echo "actions:"
	echo "	`basename $0`	Show information"
	echo "	addAgentToNetwork.sh	Add to OpenDomo"
	echo

	echo "#> Old agents:"
	echo "list:`basename $0`"
	for agent in `cat $ALIST | grep "AGENT-DOWN" | cut -f1 -s`; do
		echo "	-$agent	$agent	odagent"
	done
	echo "actions:"
	echo "	`basename $0`	Show information"
	echo "	removeAgent.sh	Remove agent"
	echo


else
	# -------------------------------------------------------------------------
	# Get Agent Info
	# -------------------------------------------------------------------------
	echo "# Agent [$1] information:"
	if grep -q $1 $HOME/.ssh/known_hosts; then
		#TODO Display some information
      RESULT=`$REXEC $1 'cat /etc/opendomo/uid' | grep [0-9A-F]`
      echo "# uid: $RESULT"
	else
		echo "#ERR This host does not belong to OpenDomo's network"
	fi
fi
echo
exit 0

